function generateOrder(){
    var p = shopCar[0];
    location = 'http://localhost:8080/jsp1/zfbpay?' +
        'price='+p.price+'&' +
        'subject='+"Under Armour"+'&' +
        'body=Under Armour';
}

var noneSelectImgSrc = "";
var selectImgSrc = "";
var shopCar;

allSelectImg.onclick = function(){
    this.src = this.src == selectImgSrc?
        noneSelectImgSrc:selectImgSrc;

    var allSelectImgSrc = this.src;

    $(".ckimg").each(function(index,ele){
        ele.src = allSelectImgSrc;
    });
    totalValueofCalculation()
};

window.onload = function() {
    $.showPreloader();
    $("#shopCarDiv").hide();
    var loadedImageCount = 0;

    shopCar = JSON.parse(localStorage.getItem("shopCar"));
    if (!shopCar||shopCar.length==0) {
        emptyCar.style.display = "block";
        $.hidePreloader();
    } else {
        shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
        for (var i = 0; i < shopCar.length; i++) {
            var product = shopCar[i];
            var productDiv = document.createElement("div");
            productDiv.className = "product";
            productDiv.style.background = "white";
            productDiv.style.marginTop = "10px";

            var selectImg = document.createElement("img");
            selectImg.src = noneSelectImgSrc;
            selectImg.style.height = "20px";
            selectImg.className = "ckimg";
            selectImg.onclick = function(){

                if(this.src == selectImgSrc){
                    this.src = noneSelectImgSrc;
                }else{
                    this.src = selectImgSrc;
                }
                totalValueofCalculation()
            };

            var productImage = document.createElement("img");
            productImage.src = product.pic;
            productImage.style.height = "100px";
            productImage.onload = function(){
                loadedImageCount++;
                if(loadedImageCount==shopCar.length){
                    $.hidePreloader();
                    $("#shopCarDiv").show(300);
                }
            };

            var productInfoDiv = document.createElement("div");

            var titleDiv = document.createElement("div");
            titleDiv.className = "titleDiv";
            titleDiv.innerHTML = product.title;

            var priceAndCountDiv = document.createElement("div");
            priceAndCountDiv.className = "priceAndCountDiv";

            var priceSpan = document.createElement("span");
            priceSpan.className = "price";
            priceSpan.innerHTML = "&yen; " + product.price;



            priceAndCountDiv.appendChild(priceSpan);


            productInfoDiv.appendChild(titleDiv);
            productInfoDiv.appendChild(priceAndCountDiv);


            var jianBtn = document.createElement("input");
            jianBtn.type = "button";
            jianBtn.className = "minus";
            jianBtn.value = "-";
            jianBtn.setAttribute("name","minus");
            jianBtn.style.textAlign = "center";
            jianBtn.style.width = "40px";
            jianBtn.style.background = "none";
            jianBtn.style.borderTop = "none";
            jianBtn.style.borderLeft = "none";
            jianBtn.style.borderRight = "none";
            jianBtn.style.borderBottom = "1px solid white";
            jianBtn.style.borderBottomColor = "white";
            productInfoDiv.appendChild(jianBtn);


            var inputDiv = document.createElement("input");
            inputDiv.className = "amount";
            inputDiv.value = "1";
            inputDiv.type = "text";
            inputDiv.setAttribute("name","amount");
            inputDiv.value = product.count;
            inputDiv.style.width = "50px";
            inputDiv.style.textAlign = "center";
            inputDiv.style.borderTop = "none";
            inputDiv.style.borderBottom = "1px solid white";
            inputDiv.style.borderLeft = "1px solid white";
            inputDiv.style.borderRight = "1px solid white";
            inputDiv.style.background = "#efeff4";
            productInfoDiv.appendChild(inputDiv);

            var jiaBtn = document.createElement("input");
            jiaBtn.className = "plus";
            jiaBtn.type = "button";
            jiaBtn.value = "+";
            jiaBtn.setAttribute("name","plus");
            jiaBtn.style.textAlign = "center";
            jiaBtn.style.width = "40px";
            jiaBtn.style.background = "none";
            jiaBtn.style.borderTop = "none";
            jiaBtn.style.borderLeft = "none";
            jiaBtn.style.borderRight = "none";
            jiaBtn.style.borderBottom = "1px solid white";
            jiaBtn.style.borderBottomColor = "white";
            productInfoDiv.appendChild(jiaBtn);

            var deleteimg = document.createElement("img");
            deleteimg.src = "../Images/delete.png";
            deleteimg.style.width = "15px";
            deleteimg.style.float = "right";
            deleteimg.style.marginRight = "13px";
            productInfoDiv.appendChild(deleteimg);
            deleteimg.nid = product.nid;
            deleteimg.onclick = function(){
                deleteProductFromShopCart(this,this.nid);
            };

            productDiv.appendChild(selectImg);
            productDiv.appendChild(productImage);
            productDiv.appendChild(productInfoDiv);
            shopCarDiv.appendChild(productDiv);
        }
    }
    f1();
}



function deleteProductFromShopCart(deleteBtn,nid){


    for(var i=0;i<shopCar.length;i++){
        // 如果购物车对象中已经存在了这个商品
        if(shopCar[i].nid == nid){
            $.confirm('您确定要移除该宝贝吗?', function () {
                shopCar.splice(i,1);
                console.log(i);
                $(deleteBtn).parents(".product").css("height",
                    $(deleteBtn).parents(".product").height());
                setTimeout(function(){
                    $(deleteBtn).parents(".product").css("opacity",0);
                    $(deleteBtn).parents(".product").css("height",0);
                },1);
                shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
                $.toast("移除成功！");
                if(shopCar.length==0){
                    emptyCar.style.display = "block";
                }
                // 存进本地存储，替换原来的购物车信息
                localStorage.setItem("shopCar",JSON.stringify(shopCar));
            });

            break;
        }
    }
}

function f1() {
    var plusbtns = document.getElementsByClassName("plus");
    var amounts = document.getElementsByClassName("amount");
    var jianfas = document.getElementsByClassName("minus");
    var prices = document.getElementsByClassName("price");

for(var i=0;i<plusbtns.length;i++){
    plusbtns[i].index = i;
    plusbtns[i].onclick=function (){
        var j=this.index;
        amounts[j].value = parseInt(amounts[j].value)+1;

        totalValueofCalculation();
    }
}

for(var i=0;i<jianfas.length;i++) {
    jianfas[i].index = i;
    jianfas[i].onclick = function () {
        var j = this.index;
        if(amounts[j].value<2){
            return;
        }
        amounts[j].value = parseInt(amounts[j].value) - 1;
        totalValueofCalculation();
        }
    }
}
totalValueofCalculation();
var cont = document.getElementsByClassName("product");
function   totalValueofCalculation() {
    var plusbtns = document.getElementsByClassName("plus");
    var amounts = document.getElementsByClassName("amount");
    var jianfas = document.getElementsByClassName("minus");
    var prices = document.getElementsByClassName("price");
    var total = 0;
    for(var i=0;i<plusbtns.length;i++){
        if(cont[i].firstElementChild.src == selectImgSrc){
        var jiage = parseFloat(prices[i].innerHTML.substring(1))*parseInt(amounts[i].value);
        total=total + jiage;
        }
    }
    totalPrices.innerHTML = "&yen;"+total.toFixed(2)
}

f1();